﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace SilverlightCoverage
{    
    public class Converter : IValueConverter
    {
        ColorConverter cCon = null;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            SolidColorBrush brush = null;
            string va = value.ToString();

            if (va == "NC")
            {
                cCon = new ColorConverter();                
                brush = new SolidColorBrush(cCon.GetColorFromHex("#FFFFB6C1"));
            }

            else if (va == "C")
            {
                cCon = new ColorConverter();
                brush = new SolidColorBrush(cCon.GetColorFromHex("#FF90EE90"));

            }
            else if (va == "PC")
            {
                cCon = new ColorConverter();
                brush = new SolidColorBrush(cCon.GetColorFromHex("#FF6A5ACD"));

            }

            return brush;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

    public class ColorConverter
    {
        public ColorConverter()
        {
        }

        public Color GetColorFromHex(string hexColor)
        {
            try
            {
                if (hexColor.StartsWith("#"))
                {
                    return Color.FromArgb(
                    Convert.ToByte(hexColor.Substring(1, 2), 16),
                    Convert.ToByte(hexColor.Substring(3, 2), 16),
                    Convert.ToByte(hexColor.Substring(5, 2), 16),
                    Convert.ToByte(hexColor.Substring(7, 2), 16)
                    );
                }
                else
                {
                    return Color.FromArgb(
                    Convert.ToByte(hexColor.Substring(0, 2), 16),
                    Convert.ToByte(hexColor.Substring(2, 2), 16),
                    Convert.ToByte(hexColor.Substring(4, 2), 16),
                    Convert.ToByte(hexColor.Substring(5, 2), 16)
                    );
                }
            }
            catch
            {
                throw;
            }
        }

    }
}
